package com.zq.qq2.thread;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.zq.qq2.Constant;
import com.zq.qq2.FileUtils;
import com.zq.qq2.libsupersu.RootUtil;
import com.zq.qq2.service.FloatQQService;

import org.json.JSONObject;

import java.io.File;

/**
 * Created by xuebiao on 16/3/21.
 */
public class Startup extends Thread {
    private Context context;
    private Handler handler;

    public Startup( Context context, Handler handler ) {
        this.context = context;
        this.handler = handler;
    }
    public void run() {
        Constant.Debug("读取QQ账号密码文件");

        for( String xposed : new String[]{"/storage/sdcard0/zhangqing/shouji.txt"} ) {
            File xf = new File(xposed);
            if ( xf.exists() && FloatQQService.QQAccount.equals("") ) {
                try {
                    JSONObject json = new JSONObject(FileUtils.read(xposed));
                    String qq = json.getString("QQ");
                    String qqpwd = json.getString("QQPwd");
                    if ( qq != null && !qq.trim().equals("") && qqpwd != null && !qqpwd.trim().equals("") ) {
                        FloatQQService.QQAccount = qq.trim();
                        FloatQQService.QQPasswd = qqpwd.trim();
                    }
                } catch (Exception e) {

                }
            }
        }
        if ( FloatQQService.QQAccount.equals("") ) {
            Constant.Debug("QQ有问题,确认数据");
            Message msg = new Message();
            msg.what = 10;
            msg.obj = "QQ有问题,确认数据";
            handler.sendMessage(msg);
            return;
        }

        Constant.Debug("启动QQ程序");
        RootUtil.Get().BashExecute("am start -n com.tencent.mobileqq/.activity.SplashActivity");
        Constant.Debug("监测QQ是否加载完毕");
        // 如果已经登录则直接退出
        int errorNumber = 1;
        do {
            try {
                sleep(1000);
            } catch (InterruptedException e) {

            }

            for( int x = 1; x <= 3; x ++ ) {
                RootUtil.Get().BashExecute("input tap 440 40");
                RootUtil.Get().BashExecute("input tap 120 760");
            }
            if ( errorNumber ++ > 60 )
                break;
        } while(true);
        Constant.Debug("QQ加载完毕");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Constant.Debug(String.format("输入账号%s,密码:%s", FloatQQService.QQAccount, FloatQQService.QQPasswd));

        RootUtil.Get().BashExecute("input tap 242 332");
        RootUtil.Get().BashExecute("input text " + FloatQQService.QQAccount.trim());


        RootUtil.Get().BashExecute("input tap 254 400");
        RootUtil.Get().BashExecute("input text " + FloatQQService.QQPasswd.trim());

        RootUtil.Get().BashExecute("input tap 356 496");
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, "开始启动识别验证码", Toast.LENGTH_LONG).show();
            }
        });
        // ---- Login.start() ---//
        handler.sendEmptyMessage(1);
    }
}
